----------------------------------------------------------------------------------------------------------------------------------
--[[

     

]]--
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_tag_sys = Class(function(self, inst)
    self.inst = inst

    self.tags = {}
    self._net_flag = false
    self.__net_json = net_string(inst.GUID,"loramia_com_tag_sys","loramia_com_tag_sys")
    if not TheNet:IsDedicated() then
        self.inst:ListenForEvent("loramia_com_tag_sys",function()
            local str = self.__net_json:value()
            local crash_flag, data = pcall(json.decode,str)
            if crash_flag then
                self.tags = data.tags or {}
            end
            self.inst:PushEvent("refreshcrafting")
        end)
    end
    if TheWorld.ismastersim then
        self.inst:DoPeriodicTask(2,function()
            self:SendData2Client(self.tags)
        end)
        self.inst:ListenForEvent("newstate",function()
            self:SendData2Client(self.tags)
        end)
    end
end,
nil,
{

})
------------------------------------------------------------------------------------------------------------------------------
----- event push
    function loramia_com_tag_sys:EventPush()
        self.inst:PushEvent("loramia_com_tag_sys.refresh")        
    end
------------------------------------------------------------------------------------------------------------------------------
------
    function loramia_com_tag_sys:SendData2Client(tags)
        local new_table = {}
        for tag, flag in pairs(tags) do
            if tag and flag then
                new_table[tag] = true
            end
        end
        self.tags = new_table
        if TheWorld.ismastersim then
            local send_data = {
                tags = new_table,
                flag = self._net_flag,
            }
            self.__net_json:set(json.encode(send_data))
            self._net_flag = not self._net_flag
        end
        self:EventPush()
    end
------------------------------------------------------------------------------------------------------------------------------
---- AddTag/RemoveTag
    function loramia_com_tag_sys:AddTag(tag_name)
        self.tags[tag_name] = true
        self:EventPush()
    end
    function loramia_com_tag_sys:RemoveTag(tag_name)
        self.tags[tag_name] = false
        self:EventPush()
    end
------------------------------------------------------------------------------------------------------------------------------
---- HasTag
    function loramia_com_tag_sys:HasTag(tag_name)
        if TheWorld.ismastersim then
            return self.inst.components.loramia_com_tag_sys:HasTag(tag_name)
        end
        return self.tags[tag_name] or false
    end
    function loramia_com_tag_sys:HasOneOfTags(temp,...)
        if TheWorld.ismastersim then
            return self.inst.components.loramia_com_tag_sys:HasOneOfTags(temp,...)
        end

        local _temp_tags = nil
        if type(temp) == "table" then
            _temp_tags = temp
        else
            _temp_tags = {temp,...}
        end

        for _,tag_name in pairs(_temp_tags) do
            if self.tags[tag_name] then
                return true
            end
        end
        return false
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_tag_sys







